package kr.blogspot.stiner01.moduletutorial;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SoundPoolTest extends Activity implements OnTouchListener 
{
    StringBuilder _builder = new StringBuilder();
    SoundPool _soundPool;
    int _explosionId = -1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setOnTouchListener(this);
        setContentView(textView);
        
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        _soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
        
        try
        {
            AssetManager assetManager = getAssets();
            AssetFileDescriptor descriptor = assetManager.openFd("explosion.ogg");
            _explosionId = _soundPool.load(descriptor, 1);
        }
        catch (IOException e)
        {
            _builder.append("Couldn't load sound effect from asset, " + e.getMessage() + "\n");
        }
        
        _builder.append(_explosionId);
        
        textView.setText(_builder.toString());
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            if (_explosionId != -1)
            {
                _soundPool.play(_explosionId, 1, 1, 0, 0, 1);
            }
        }
        
        return true;
    }
}
